package com.xc.quickstart.manager.sso;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.quickstart.api.query.LoginQuery;
import com.xc.quickstart.common.annotation.LogAround;
import com.xc.quickstart.manager.sso.model.*;
import com.xc.quickstart.service.dto.DeptInfoDTO;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.codec.Decoder;
import feign.gson.GsonDecoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@FeignClient(value = "SSO-client", url = "${sso.root_path}", configuration = SSOClient.SSOClientConfig.class)
interface SSOClient {

    @Slf4j
    class SSOClientConfig implements RequestInterceptor {

        @Value("${sso.api_app_id}")
        private String apiAppId;

        @Value("${sso.api_app_secret}")
        private String apiAppSecret;

        @Bean
        public Decoder decoder() {
            //配置json 的解码器,使用gson的解码器
            return new GsonDecoder();
        }

        @Bean
        //配置打印日志
        public feign.Logger.Level multipartLoggerLevel() {
            return feign.Logger.Level.FULL;
        }

        @Override
        public void apply(RequestTemplate requestTemplate) {
            String method = requestTemplate.method();
            requestTemplate.header("appId",apiAppId);
            requestTemplate.header("secret",apiAppSecret);
            if ("POST".equals(method)) {
                Request.Body body = requestTemplate.requestBody();
                String jsonLine = body.asString();
                JSONObject jsonObject = JSON.parseObject(jsonLine);
                Map<String, Object> map = new HashMap<>();
                jsonObject.forEach(map::put);
                map.put("client_id", apiAppId);
                Request.Body newBody = Request.Body.encoded(JSON.toJSONString(map).getBytes(), Charset.defaultCharset());
                requestTemplate.body(newBody);
            }
        }
    }

    /**
     * 获取已离职员工
     *
     * @param
     * @return
     */
    @PostMapping(value = "/user/getDimissionUser", consumes = MediaType.APPLICATION_JSON_VALUE)
    FeignSSOResponseModel<List<DimissionUserModel>> getDimissionUser(@RequestBody SSOPageModel ssoPageModel);

    /**
     * 根据权限功能key来获取具有该权限的员工数据 ,返回员工id、工号、姓名
     *
     * @param code
     * @return
     */
    @GetMapping(value = "/private/inner/queryUserByResourceCode", consumes = MediaType.APPLICATION_JSON_VALUE)
    FeignSSOResponseModel<List<SSOUserByCodeModel>> queryUserByResourceCode(@RequestParam("code") String code);


    /**
     * 获取验证码
     *
     * @param mobile
     * @return
     */
    @GetMapping(value = "/login/get_sms_code", consumes = MediaType.ALL_VALUE)
    FeignSSOResponseModel<T> getSmsCode(@RequestParam("mobile") String mobile);


    /**
     * 登陆
     *
     * @param loginDTO
     * @return
     */
    @PostMapping(value = "/login/code_login", consumes = MediaType.APPLICATION_JSON_VALUE)
    Object login(@RequestBody LoginQuery loginDTO);


    /**
     * 获取用户权限
     *
     * @param ssoUserInfoReq
     * @return
     */
    @PostMapping(value = "/token/get_user_allInfo_crm", consumes = MediaType.APPLICATION_JSON_VALUE)
    @LogAround
    FeignSSOResponseModel<UserAuthorityModel> getUserAuthorityModel(@RequestBody SSOUserInfoReq ssoUserInfoReq);


    /**
     * 根据工号查询该员工有哪些部门的数据权限,返回部门Id，部门名称
     *
     * @param jobNumber
     * @return
     */
    @GetMapping(value = "/private/inner/queryDataPermissionByJobnumber", consumes = MediaType.APPLICATION_JSON_VALUE)
    @LogAround
    FeignSSOResponseModel<List<DeptInfoDTO>> getDeptInfoDTOList(@RequestParam("jobnumber") String jobNumber);

    /**
     * 根据有部门列表权限的员工
     *
     * @param deptDataPermissionQuery
     * @return
     */
    @LogAround
    @PostMapping(value = "/private/inner/listAllDataPermissionUserByDeptIds", consumes = MediaType.APPLICATION_JSON_VALUE)
    FeignSSOResponseModel<List<DeptDataPermissionModel>> listAllDataPermissionUserByDeptIds(@RequestBody DeptDataPermissionQuery deptDataPermissionQuery);
}
